/**
 * Copyright 2013 Net355 Co,.Ltd. 
 * All rights reserved.
 *
 * "StringUtil.java is for what: "
 * 
 * Create by Ray at 2013-12-16 上午10:05:54.
 * Ray [email:rayinhangzhou@126.com]
 * 
 * Revision History
 *
 * Date            Programmer                   Notes
 * ---------    ---------------------  -----------------------------------
 * 2013-12-16            Ray                       initial
 **/


package com.net355.eblog.util;

import java.io.File;

import org.springframework.util.StringUtils;

public class StringUtil extends StringUtils {

	public static String parseHTMLtoText(String html) {
		html = html.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll("<[^>]*>", "")
				.replaceAll("[(/>)<]", "");
		return html;
	}

	public static String pathReplace(String s) {
		return s.replaceAll("//", System.getProperty(File.separator));
	}
	
	public static String transHtml(String content) {
		if(content==null) return "";        
		String html = content;
		html = StringUtils.replace(html, "'", "&apos;");
		html = StringUtils.replace(html, "\"", "&quot;");
		html = StringUtils.replace(html, "\t", "&nbsp;&nbsp;");// 替换跳格
		//html = StringUtils.replace(html, " ", "&nbsp;");// 替换空格
		html = StringUtils.replace(html, "<", "&lt;");
		html = StringUtils.replace(html, ">", "&gt;");
		return html;
	}
	
	public static String getSubContent(String content, int index) {
		if(index>content.length()) {
			return content;
		} else {
			return content.substring(0, index);
		}
	}
	
	public static String bytesToHex(byte[] bytes) {
		StringBuilder sb = new StringBuilder();
		for (byte hashByte : bytes) {
			int intVal = 0xff & hashByte;
			if (intVal < 0x10) {
				sb.append('0');
			}
			sb.append(Integer.toHexString(intVal));
		}
		return sb.toString();
	}

}
